<?php
namespace Modules\Topic\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Topic\Entities\Topic;

use Validator;
use ThrowException;
use Auth;

class TopicController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $options['order_by'] = ['set_top_at' => 'desc', 'list_weight' => 'desc'];

        $options['with'] = ['user', 'topic_tag'];
        $options['with_count'] = ['comments'];
        $options['scopes'] = ['onStock', 'mainTopic'];

        $options['only'] = [
            'uuid',
            'title',
            'thumb',
            'text',
            'images',
            'topic_tag',
            'is_liked',
            'comments_count',
            'created_at',
            'set_top_at',
            'user' => [
                'uuid',
                'name',
                'headimg',
            ]
        ];

        $list = Topic::getList($options);

        $user = Auth::user();
        $list->each(function ($item) use ($user) {
            $item->is_liked = $item->isLikedBy($user);
        });

        return $list->generateListResponse();
    }

    public function store(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $info = $req->all();

        $status = 0;

        $info['user_id'] = $user->id;

        $info['status'] = $status;

        $item = Topic::create($info);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid,
            'status' => $status
        ];
    }

    public function show(Request $req, $uuid)
    {
        $item = Topic::getEntity([
            'uuid' => $uuid,
            'with'  => ['user', 'topic_tag']
        ]);

        $only = [
            'id',
            'uuid',
            'title',
            'thumb',
            'text',
            'images',
            'topic_tag',
            'is_liked',
            'sub_topics_count',
            'created_at',
            'set_top_at',
            'user' => [
                'uuid',
                'name',
                'headimg',
            ]
        ];

        $info = $item->getInfo($only);

        $info['is_liked'] = $item->isLikedBy(Auth::user());

        return [
            'info' => $info
        ];
    }

    public function action(Request $req, $uuid)
    {
        $rule = [
            'type' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Topic::getEntity([
            'uuid' => $uuid
        ]);

        if ($req->type === 'like_or_cancel') {
            $user = Auth::requireLoginUser();
            $item->likeOrUnlike($user);
        }

        return [];
    }

}
